package tech.yunqian.slim.project.service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

import com.alibaba.fastjson.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class NotificationService {
    @Value("${notification.sms.url}")
    private String smsUrl;

    @Value("${notification.voice.url}")
    private String voiceUrl;

    @Value("${notification.email.url}")
    private String emailUrl;

    @Value("${notification.wechat.url}")
    private String wechatUrl;

    @Autowired
    private AsyncHttpRequestService asyncHttpRequestService;

    public JSONObject sendSms(JSONObject request) {
        JSONObject result = null;
        List<String> headers = new ArrayList<>();
        Future<String> future = asyncHttpRequestService.post(smsUrl, headers, request);
        String response = null;
        try {
            response = future.get();
            log.info(response);
        } catch (Exception e) {
            log.info(e.getMessage());
            StackTraceElement[] traces = e.getStackTrace();
            for (StackTraceElement trace : traces) {
                log.debug(trace.toString());
            }
        }
        return result;
    }

    public JSONObject sendVoice(JSONObject request) {
        JSONObject result = null;
        List<String> headers = new ArrayList<>();
        Future<String> future = asyncHttpRequestService.post(voiceUrl, headers, request);
        String response = null;
        try {
            response = future.get();
            log.info(response);
        } catch (Exception e) {
            log.info(e.getMessage());
            StackTraceElement[] traces = e.getStackTrace();
            for (StackTraceElement trace : traces) {
                log.debug(trace.toString());
            }
        }
        return result;
    }

    public JSONObject sendEmail(JSONObject request) {
        JSONObject result = null;
        List<String> headers = new ArrayList<>();
        Future<String> future = asyncHttpRequestService.post(emailUrl, headers, request);
        String response = null;
        try {
            response = future.get();
            log.info(response);
        } catch (Exception e) {
            log.info(e.getMessage());
            StackTraceElement[] traces = e.getStackTrace();
            for (StackTraceElement trace : traces) {
                log.debug(trace.toString());
            }
        }
        return result;
    }

    public JSONObject sendWechat(JSONObject request) {
        JSONObject result = null;
        List<String> headers = new ArrayList<>();
        Future<String> future = asyncHttpRequestService.post(wechatUrl, headers, request);
        String response = null;
        try {
            response = future.get();
            log.info(response);
        } catch (Exception e) {
            log.info(e.getMessage());
            StackTraceElement[] traces = e.getStackTrace();
            for (StackTraceElement trace : traces) {
                log.debug(trace.toString());
            }
        }
        return result;
    }
}
